package com.stephen.mymall.entity.vo;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.stephen.mymall.enums.ResultEnum;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;


/**
 * @ author: stephen
 * @ create: 2020-03-17 21:06
 * @ description: 返回前端的统一数据格式
 **/
@Getter
@Setter
@ToString
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Result<T> {

    private Integer code;

    private String msg;

    private T data;

    public Result() {

    }

    public Result(Integer code,String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Result(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static<T> Result<T> result(Integer code, String msg, T data) {
        return new Result<>(code,msg,data);
    }

    public static<T> Result<T> result(Integer code, String msg) {
        return new Result<>(code,msg,null);
    }

    public static<T> Result<T> result(ResultEnum resultEnum, T data) {
        return result(resultEnum.getCode(),resultEnum.getDescription(),data);
    }

    public static<T> Result<T> result(ResultEnum resultEnum) {
        return result(resultEnum,null);
    }

    public static<T> Result<T> success(ResultEnum resultEnum) {
        return result(resultEnum,null);
    }



    public static <T> Result<T> success() {
        return result(ResultEnum.SUCCESS.getCode(),ResultEnum.SUCCESS.getDescription(),null);
    }

    public static <T> Result<T> success(String msg) {
        return result(ResultEnum.SUCCESS.getCode(),msg);
    }

    public static <T> Result<T> success(T data) {
        return result(ResultEnum.SUCCESS.getCode(),ResultEnum.SUCCESS.getDescription(),data);
    }

    public static <T> Result<T> error() {
        return result(ResultEnum.SYSTEM_ERROR.getCode(),ResultEnum.SYSTEM_ERROR.getDescription());
    }
    public static<T> Result<T> error(ResultEnum resultEnum) {
        return result(resultEnum);
    }

    public static <T> Result<T> error(String msg) {
        return result(ResultEnum.SYSTEM_ERROR.getCode(),msg);
    }

    public static <T> Result<T> error(ResultEnum resultEnum,String msg) {
        return result(resultEnum.getCode(),msg);
    }

}
